package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Dashboard;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DashboardMapper {

    @Insert("INSERT INTO dashboard (`tenant_id`, `name`) VALUES (#{tenantId}, #{name})")
    void create(@Param("tenantId") long tenantId, @Param("name") String name);

    @Select("<script>" +
            "SELECT * FROM `dashboard` " +
            "<where>" +
            " `tenant_id`=#{dashboard.tenantId} " +
            "<if test='dashboard.id!=null'> AND `id`!=#{dashboard.id}</if>" +
            "<if test='dashboard.name!=null'> AND `name` LIKE CONCAT('%',#{dashboard.name},'%') </if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Dashboard> find(@Param("dashboard") Dashboard dashboard, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `dashboard` " +
            "<where>" +
            " `tenant_id`=#{dashboard.tenantId} " +
            "<if test='dashboard.id!=null'> AND `id`!=#{dashboard.id}</if>" +
            "<if test='dashboard.name!=null'> AND `name` LIKE CONCAT('%',#{dashboard.name},'%') </if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("dashboard") Dashboard dashboard);

    @Update("UPDATE dashboard SET `name` = #{dashboard.name} WHERE `id` = #{dashboard.id} AND `tenant_id` = #{dashboard.tenantId}")
    int update(@Param("dashboard") Dashboard dashboard);

    @Update("UPDATE `dashboard` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("tenantId") long tenantId, @Param("id") long id);

    @Select("<script>" +
            "SELECT * FROM `dashboard` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='dashboardIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<Dashboard> findDashboardByIds(@Param("dashboardIds") List<Long> dashboardIds);
}
